<?php
$pagename = is_category() ? 'category' : (is_single() ? 'single' : 'index');
$tdk = [
    'index' => [
        'title' => get_bloginfo('name'),
        'description' => get_bloginfo('description'),
    ],
    'category' => is_category() ? [
        'title' => get_category(get_queried_object_id())->name . '-' . get_bloginfo('name'),
    ] : [],
    'single' => is_single() ? [
        'title' => get_the_title(),
        'description' => get_the_excerpt(),
    ] : [],
];


function setTdk(array $seo): string
{
    $tags = [
        'title' => isset($seo['title']) && !empty($seo['title']) ? "<title>{$seo['title']}</title>" : '',
        'description' => isset($seo['description']) && !empty($seo['description']) ? "<meta name='description' content='{$seo['description']}'>" : '',
        'keywords' => isset($seo['keywords']) && !empty($seo['keywords']) ? "<meta name='keywords' content='{$seo['keywords']}'>" : '',
    ];
    return implode('', $tags);
}

?>
<!DOCTYPE html>
<html lang="ar">

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <?= setTdk($tdk[$pagename] ?? []) ?>
</head>

<body>
    <?php get_template_part('../blocks/header4') ?>
    <main>
        <div class="container grid">
            
            <?php if ($pagename == 'index') : ?>
                <section class="mths"></section>
                <?php get_template_part('../blocks/i1') ?>
                <?php get_template_part('../blocks/imgpost2') ?>
                <?php get_template_part('../blocks/i2') ?>
                <?php get_template_part('../blocks/imgpost3') ?>
               
                <?php get_template_part('../blocks/i2') ?>
             
            <?php endif ?>

            <?php if ($pagename == 'category') : ?>
                <?php
                $data = get_latest_posts_by_category('', 0, 24);
                echo get_template_part('../blocks/i2', null, ['posts' => $data['posts'], 'column' => 3]);
                echo get_template_part('../blocks/page1', null, ['data' => $data['pagination']]);
                ?>
            <?php endif ?>

            <?php if ($pagename == 'single') : ?>
                <?php
                    $path = explode('/', $_SERVER['REQUEST_URI']);
                    $currentPost = formatPost(intval(end($path)));
                ?>
                <?= get_template_part('../blocks/c1',null,['currentPost' => $currentPost]) ?>
                <?= get_template_part('../blocks/i2', null, ['pagesize' => 8]); ?>
            <?php endif ?>
        </div>
    </main>
    <?php get_template_part('../blocks/footer1') ?>
</body>

</html>